package com.su.song.shapapplication;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.su.song.shapapplication.util.ViewUtil;

import java.io.PrintWriter;
import java.math.RoundingMode;
import java.util.Random;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_login_password;
    private EditText et_login_password;
    private CheckBox ck_rember;
    private Button btn_forget;
    private ActivityResultLauncher<Intent> regisfter;
    private String phone;
    private EditText et_phone;
    private RadioButton rg_password;
    private RadioButton rg_verificode;
    private ActivityResultLauncher<Intent> register;
    private String mVerificode;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //获取登录密码文本
        tv_login_password = findViewById(R.id.tv_login_password);
        //获取登录提示文本
        et_login_password = findViewById(R.id.et_login_password);
        //获取记住密码按钮
        ck_rember = findViewById(R.id.ck_rember);
        //获取忘记密码按钮
        btn_forget = findViewById(R.id.btn_forget_password);
        et_phone = findViewById(R.id.et_phone_input);
        //给密码或验证码按钮设置监听事件
        btn_forget.setOnClickListener(this);
        //设置两个按钮
        rg_password = findViewById(R.id.rg_password);
        rg_verificode = findViewById(R.id.rg_verificode);
        //监听总按钮
        RadioGroup rg_login2 = findViewById(R.id.rg_login2);
        rg_login2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    //当点击了密码登录
                    case R.id.rg_password:
                        tv_login_password.setText(getString(R.string.login_password));
                        et_login_password.setHint(getString(R.string.login_password_input));
                        btn_forget.setText(getString(R.string.forget_password));
                        ck_rember.setVisibility(View.VISIBLE);
                        et_login_password.setText(null);
                        break;
                    //等点击了验证码登录
                    case R.id.rg_verificode:
                        tv_login_password.setText(getString(R.string.verificode));
                        et_login_password.setHint(getString(R.string.verificode_input));
                        btn_forget.setText(getString(R.string.verificode_get));
                        ck_rember.setVisibility(View.GONE);
                        et_login_password.setText(null);
                        break;
                }
            }
        });


        et_phone.addTextChangedListener(new HideTextWater(et_phone,11));
        et_login_password.addTextChangedListener(new HideTextWater(et_login_password,6));
        //监听输入不完全时，弹出提示
        et_phone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    phone = et_phone.getText().toString();
                    if(TextUtils.isEmpty(phone) || phone.length()<11){
                        et_phone.requestFocus();
                        Toast.makeText(LoginActivity.this,"请输入11位手机号",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });


      //接受函数
        register = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {

            }
        });


    }

    @Override
    public void onClick(View v) {
//        当密码登录点击忘记密码时，调转到忘记密码界面
        if(rg_password.isChecked()){
            phone = et_phone.getText().toString();
            Intent intent = new Intent(this,LoginForgetAxtivity.class);
            intent.putExtra("phone", phone);
            register.launch(intent);
        }
        if(rg_verificode.isChecked()){
            //生成六位验证码
            mVerificode = String.format("%d",new Random().nextInt(999999));
            phone = et_phone.getText().toString();
            //弹出提示框，让用户记住验证码
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("尊敬的用户");
            builder.setMessage("手机号：" + phone + "\n验证码：" + mVerificode+"\n请输入相关验证码");
            builder.setPositiveButton("好的",null);
            Dialog dialog = builder.create();
            dialog.show();
        }
    }

    //收起软件盘函数
    private class HideTextWater implements TextWatcher {
        private EditText mView;//声明一个编辑框对象
        private int MaxtLength;//声明一个最大长度变量

        public HideTextWater(EditText v, int MaxLength) {
            this.mView = v;
            this.MaxtLength = MaxLength;//初始化
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            String str = s.toString();
            if(str.length() == MaxtLength){
                ViewUtil.HideInputMethod(LoginActivity.this,mView);
            }
        }
    }





}
//